#include"string.h"

namespace My
{
	const size_t string::npos = -1;

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tem = new char[n + 1];
			strcpy(tem, _str);
			delete[] _str;
			_str = tem;
			_capacity = n;
		}
	}
	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity==0?4:_capacity * 2);
		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}
	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}

	string& string::operator +=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator +=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		/*int end = _size;
		while (end >= (int)pos)
		{
			_str[end+1] = _str[end];
			end--;
		}*/

		size_t end = _size+1;
		while (end > pos)
		{
			_str[end] = _str[end-1];
			end--;
		}
		_str[pos] = ch;
		_size++;
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}

		size_t end = _size+len;
		while (end-len+1 > pos)
		{
			_str[end] = _str[end - len];
			end--;
		}

		for (size_t i = 0; i < len; i++)
		{
			_str[pos+i] = str[i];
		}
		
		//strcat(_str + pos, str);
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		if (_size - pos <= len)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			while (pos+len  < _size)
			{
				_str[pos] = _str[pos + len];
				pos++;
			}
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}
	size_t string::find(char* str, size_t pos)
	{
		assert(pos<_size);

		const char* ptr = strstr(_str + pos, str);
		if (ptr == nullptr)
		{
			return npos;
		}
		else
		{
			return ptr - _str;
		}
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		
		if (len > _size - pos)
		{
			len = _size - pos;
		}
		string s;
		s.reserve(len);
		/*for (size_t i = 0; i < len; i++)
		{
			s += _str[pos + i];
		}*/

		strncpy(s._str, _str + pos, len);
		s._str[len] = '\0';
		return s;
	}
	bool operator<(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) < 0;
	}
	bool operator<=(const string& s1, const string& s2)
	{
		return s1 == s2 && s1 < s2;
	}
	bool operator>(const string& s1, const string& s2)
	{
		return !(s1 <= s2);
	}
	bool operator>=(const string& s1, const string& s2)
	{
		return !(s1 < s2);
	}
	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}
	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}

	ostream& operator <<(ostream& out, const string& s)
	{
		for (auto ch: s)
		{
			out << ch;
		}
		return out;
	}

	istream& operator >>(istream& in, string& s)
	{
		s.clear();
		//s.reserve(1024);
		const int N = 256;
		char buff[N];
		int end = 0;
		char ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[end++] = ch;
			if (end == N - 1)
			{
				buff[end] = '\0';
				s += buff;
				end = 0;
			}
			ch = in.get();
		}
		if (end > 0)
		{
			buff[end] = '\0';
			s += buff;
		}
		return in;
	}

	istream& getline(istream& in, string& str)
	{
		char ch;
		ch = in.get();
		while (ch!='\n')
		{
			str += ch;
			ch = in.get();
		}

		return in;
	}
}
